package com.hama.iotrust.common.mvc;

import java.util.Iterator;
import java.util.Vector;

import javax.servlet.http.HttpSession;


public class WebSessionManager {

    private static class SessionManagerHolder {

        final static WebSessionManager INSTANCE = new WebSessionManager();
    }

    private Vector<HttpSession> sessions = new Vector<HttpSession>();

    private WebSessionManager(){
    }

    public static WebSessionManager getInstance() {
        return SessionManagerHolder.INSTANCE;
    }

    public Vector<HttpSession> getSessions() {
        return sessions;
    }

    public void add(HttpSession session) {
        sessions.add(session);
    }

    public void remove(HttpSession session) {
        sessions.remove(session);
    }

    public void remove(Long userId) {
        Iterator<HttpSession> iterator = sessions.iterator();
        while (iterator.hasNext()) {
            HttpSession session = iterator.next();
//            UserInfo userinfo = (UserInfo) session.getAttribute(Constants.LOGIN_USER);
//            if (userinfo.getSysUser().getUserId().equals(userId)) {
//                session.removeAttribute(Constants.LOGIN_USER);
//            }
        }
    }

    public boolean isAlreadyLogin(Long userId,String userIp) {
        for (int i = 0; i < sessions.size(); i++) {
            HttpSession session = sessions.elementAt(i);
            return false;
//            UserInfo userinfo = (UserInfo) session.getAttribute(Constants.LOGIN_USER);
//            if (userinfo == null) {
//                continue;
//            }
//            if (userinfo.getSysUser().getUserId().equals(userId) && !userinfo.getIp().equals(userIp)) {
//                return true;
//            }
        }
        return false;
    }
}
